package com.insurance.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.insurance.common.common.ResponseData;
import com.insurance.common.enums.CommonEnum;
import com.insurance.common.utils.JwtOperatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * token拦截器
 * @version 1.0
 * @date 2021/2/24
 **/
@Slf4j
public class AuthenticationTokenFilter extends AccessControlFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        PrintWriter out = null;
        try {
            //获取token
            if (request.getMethod().equals(RequestMethod.OPTIONS.name())) {
                return true;
            }
            String token = request.getHeader("Authorization");
            if(StringUtils.isBlank(token) || JwtOperatorUtil.isTokenExpired(token)){
                ResponseData errorResponseData = ResponseData.error(CommonEnum.TOKEN_ERROR);
                log.error("【请求未通过：{}】", request.getRequestURI());
                response.setContentType("application/json;charset=utf-8");
                out = response.getWriter();
                out.write(new ObjectMapper().writeValueAsString(errorResponseData));
                return false;
            }else{
                /*String servletPath = request.getServletPath();
                //获取权限字符串集合
                List<String> permissionList = JwtOperatorUtil.getPermissionList(request);
                if(!permissionList.contains(servletPath)){
                    ResponseData errorResponseData = ResponseData.error(CommonEnum.AUTHENTICATION_ERROR);
                    log.error("【权限不足：{}】", servletPath);
                    response.setContentType("application/json;charset=utf-8");
                    out = response.getWriter();
                    out.write(new ObjectMapper().writeValueAsString(errorResponseData));
                    return false;
                }*/
                return true;
            }
        } catch (Exception e) {
            log.error("【请求未通过：{}, 拦截器发生异常, 系统异常：{}】", request.getRequestURI(), e.getMessage());
            ResponseData errorResponseData = ResponseData.error(CommonEnum.TOKEN_ERROR);
            response.setContentType("application/json;charset=utf-8");
            out = response.getWriter();
            out.write(new ObjectMapper().writeValueAsString(errorResponseData));
            return false;
        } finally {
            if(out != null){
                out.flush();
                out.close();
            }
        }
    }
}
